﻿using GameControl;
using Newtonsoft.Json.Serialization;
using QxFramework.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace GameCombine
{
    public class CombineUIManager : LogicModuleBase, ICombineUIManager
    {
        public static Vector3Int combineTableSize = new Vector3Int(7, 7, 1);
        UIBase currentCombineUI = null;
        UIBase skipButton;
        public Vector3Int CombineTableSize => combineTableSize;

        public bool IsBenchOpening => currentCombineUI!=null;

        bool canObjectMove = false;
        public bool CanObjectMove => canObjectMove;

        public Tuple<string,Vector3Int> UserWithBenchPair
        {
            get
            {
                var tp = currentCombineUI as CombineUI;
                return new Tuple<string, Vector3Int>(tp.UserName,
                    (GameControl.GameManager.Get<GameControl.IItemInstantiateManager>().GetItemFieldByName(tp.BenchName)as PosFixedItemField) .MeshPosition);
            }
        }

        public override void OnDestroy()
        {
            base.OnDestroy();
         //   UIManager.Instance.Close(currentCombineUI);
        }

        public void CloseCombineBench()
        {
            CombineUI.Instance.GetComponent<Animator>().SetTrigger("Combine");
            AudioControl.Instance.PlaySound("绑定在start键上的开门声");
                ClockManager.Get<IItemSyncManager>().AddItemAction((currentCombineUI as CombineUI).BenchName, (Item item) =>
                {
                    item.gameObject.GetComponentInChildren<Animator>().SetTrigger("Close");
                });
            ClockManager.Get<IItemSyncManager>().AddItemAction(GameMap.GameMapManager.Get<GameMap.IBagManager>().ItemsInBench.Find(s=>s.ItemName.StartsWith("Core")).ItemName, (Item item) =>
            {
                item.GetComponent<CameraFollow>().KeyDownPreference();
            });
            canObjectMove = true;

            Launcher.Instance.StartCoroutine(WaitToAction(()=> {
                var name = (currentCombineUI as CombineUI).BenchName;
                if (ClockManager.Get<IItemSyncManager>().GetGameObjectByName(name) == null)
                {
                    (GameControl.GameManager.Get<GameControl.IItemInstantiateManager>().GetItemFieldByName(name) as BenchField).IsOpen = false;
                }
                else
                {
                    ClockManager.Get<IItemSyncManager>().GetGameObjectByName(name).GetComponent<BenchInteraction>().IsOpen = false;
                }
                UIManager.Instance.Close(currentCombineUI);
                if (skipButton)
                {
                    skipButton.gameObject.SetActive(true);
                }
                currentCombineUI = null;
            },2.0f));
        }
        IEnumerator WaitToAction(Action action,float time=0f)
        {
            UIManager.Instance.Open("Shelter", 2);


            yield return new WaitForSeconds(time);


            UIManager.Instance.Close("Shelter");
            action.Invoke();

        }
        public void OpenCombineBench(CombineUIArgs info)
        {
            currentCombineUI = UIManager.Instance.Open("CombineUI", args: info);
            if (skipButton)
            {
                skipButton.gameObject.SetActive(false);

            }
            canObjectMove = false;

            if (ClockManager.Get<IItemSyncManager>().GetGameObjectByName(info.benchName) == null)
            {
                (GameControl.GameManager.Get<GameControl.IItemInstantiateManager>().GetItemFieldByName(info.benchName) as BenchField).IsOpen = true;
            }
            else
            {
                ClockManager.Get<IItemSyncManager>().GetGameObjectByName(info.benchName).GetComponent<BenchInteraction>().IsOpen = true;
            }
       //     Debug.Log(ClockManager.Instance.GetComponent<IItemSyncManager>());
            ClockManager.Get<IItemSyncManager>().AddItemAction(info.benchName, (Item item) =>
             {
                 item.gameObject.GetComponentInChildren<Animator>().SetTrigger("Open");
             });
            MessageManager.Instance.Get<CameraController.CameraFollowType>().DispatchMessage(CameraController.CameraFollowType.Suspend,
                this, new CameraController.CameraFollowArgs {zoomSpeed=0.05f/* CombineManager.Instance.Zoom*/, siz=2.582f/*CombineManager.Instance.Size*/,pos = GameControl.GameManager.Get<GameControl.IItemInstantiateManager>().GetItemFieldByName(info.benchName).Position+new Vector3(0.05f,0.5f,0)/*CombineManager.Instance.dis*/});

            Launcher.Instance.StartCoroutine(WaitToAction(() =>
            {
                AudioControl.Instance.PlaySound("点击使拼装窗体出现按键后的声音");
            },7/6f));




        }
        public override void Init(object args)
        {
            base.Init(args);
            if (LevelManager.Instance.CurrentLevel.StartsWith("Teach"))
            {
                skipButton = UIManager.Instance.Open("Skip");


                skipButton.GetComponent<Button>().onClick.SetListener(
                    () =>
                    {
                        MessageManager.Instance.Get<GameAction>().DispatchMessage(GameAction.Skip, this, null);
                    }
                    );
            }
            else
            {
                skipButton = null;
            }
        }
    }
    interface ICombineUIManager
    {
        Vector3Int CombineTableSize { get; }
        /// <summary>
        /// 打开拼装台,item1为用户名,item2为拼装台实例名称
        /// </summary>
        /// <param name="info"></param>
        void OpenCombineBench(CombineUIArgs args);
        void CloseCombineBench();
        bool IsBenchOpening { get; }

        bool CanObjectMove { get; }
        Tuple<string,Vector3Int> UserWithBenchPair { get; }

    }

}
    public class CombineUIArgs
    {
        public string user;
        public string benchName;
   //     public bool isFinal = false;
    }